//
//  sheZhiView.m
//  qmzk
//
//  Created by tangtangchitangtang on 2017/6/30.
//  Copyright © 2017年 www.fuyuan.qmzk. All rights reserved.
//

#import "sheZhiView.h"
#import "forgetViewController.h"
#import "guanYuViewController.h"
//#import "userViewController.h"
#import "PhoneViewController.h"
#import "myViewController.h"
#import "workbenchViewController.h"
#import "homeViewController.h"
#import "Home2ViewController.h"
#import "Workbench2ViewController.h"
#import "My2ViewController.h"
@implementation sheZhiView

-(void)creatView{
    self.backgroundColor=_LightGrey;
    NSArray *nameArr=[NSArray arrayWithObjects:@"缓存清理",@"修改密码",@"更换手机号",@"关于独角兽",@"切换到专家端",nil];
    NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
    NSNumber *is_double=[userDic objectForKey:@"is_double"];
    if (is_double==0) {
        nameArr=[NSArray arrayWithObjects:@"缓存清理",@"修改密码",@"更换手机号",@"关于独角兽",nil];
    }
    else{
        NSNumber *waiter_role=[userDic objectForKey:@"waiter_role"];
        if (waiter_role.intValue==0) {
             nameArr=[NSArray arrayWithObjects:@"缓存清理",@"修改密码",@"更换手机号",@"关于独角兽",@"切换到专家端",nil];
        }
        else{
             nameArr=[NSArray arrayWithObjects:@"缓存清理",@"修改密码",@"更换手机号",@"关于独角兽",@"切换到律师端",nil];
        }
       
    }
    
    for (int i=0; i<nameArr.count; i++) {
        UIView *beiJingView2=[UIView new];
        [self addSubview:beiJingView2];
        [beiJingView2 mas_makeConstraints:^(MASConstraintMaker *make) {
      
            make.top.mas_equalTo(NAV_HEIGHT+5+45*_mainHeight*i+i*1);
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(45*_mainHeight);
        }];
        beiJingView2.backgroundColor=[UIColor whiteColor];
        beiJingView2.tag=i;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToZhangHu:)];
        [beiJingView2 addGestureRecognizer:tapGesture];
        
        UILabel *nameLabel=[UILabel new];
        [beiJingView2 addSubview:nameLabel];
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.left.mas_equalTo(_mainWidth*15);
        }];
     
        nameLabel.text=nameArr[i];
        nameLabel.font=[UIFont fontWithName:@"Helvetica" size:18];
        nameLabel.textColor=ColorWithRGB(50, 50, 50);
        
        UIImageView *jianTou=[UIImageView new];
        [beiJingView2 addSubview:jianTou];
        [jianTou mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-10);
            make.centerY.mas_equalTo(0);
            make.width.mas_equalTo(10);
            make.height.mas_equalTo(15);
        }];
        jianTou.image=[UIImage imageNamed:@"iconfont-fanhui-拷贝-3"];
    }
    
    UIButton *quRenBtn=[UIButton new];
    [self addSubview:quRenBtn];
    [quRenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(NAV_HEIGHT+300);
        make.left.mas_equalTo(_mainWidth*15);
        make.right.mas_equalTo(-_mainWidth*15);
        make.height.mas_equalTo(_mainHeight*45);
    }];
    quRenBtn.backgroundColor=_backgroundColor;
    quRenBtn.layer.cornerRadius=_mainHeight*45/2;
    [quRenBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [quRenBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    quRenBtn.titleLabel.font=[UIFont fontWithName:@"Helvetica" size:16];
    [quRenBtn addTarget:self action:@selector(pushToLogin:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)pushToZhangHu:(UITapGestureRecognizer *)tap{
    
    if (tap.view.tag==0) {
        [self creatAltel];
    }if (tap.view.tag==1) {
        forgetViewController *geRen=[[forgetViewController alloc] init];
        geRen.forgetStr=@"修改密码";
        [ZQTools pushNextViewController:self.viewController andRootController:geRen];
    }if (tap.view.tag==2) {
        PhoneViewController *geRen=[[PhoneViewController alloc] init];
        [ZQTools pushNextViewController:self.viewController andRootController:geRen];
    }if (tap.view.tag==3) {
        guanYuViewController *geRen=[[guanYuViewController alloc] init];
        [ZQTools pushNextViewController:self.viewController andRootController:geRen];
    }if (tap.view.tag==4) {
        NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
        NSNumber *waiter_role=[userDic objectForKey:@"waiter_role"];
        
        NSString *token=[userDic objectForKey:@"token"];
        NSNumber *waiter_id1=[userDic objectForKey:@"waiter_id"];
        NSString *waiter_id=[NSString stringWithFormat:@"%@",waiter_id1];
        [JPUSHService setAlias:waiter_id completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            NSLog(@"%@",iAlias);
            if (iResCode == 0) {
                NSLog(@"添加别名成功");
            }
        } seq:1];
       
        if (waiter_role.intValue==0) {
            NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"user_role",token,@"token",waiter_id,@"waiter_id", nil];
            [ZQTools AFNPostDataUrl:@"waiter/logreg/transform" Dict:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
                [NSKeyedArchiver archiveRootObject:responseObject toFile:_userModelFile];
               
                Home2ViewController *shouye=[[Home2ViewController alloc] init];
                Workbench2ViewController *huDong=[[Workbench2ViewController alloc] init];
                
                My2ViewController *youJi=[[My2ViewController alloc] init];

                UINavigationController *nav1= [[UINavigationController alloc] initWithRootViewController:shouye];
                UINavigationController *nav2= [[UINavigationController alloc] initWithRootViewController:huDong];
                UINavigationController *nav3= [[UINavigationController alloc] initWithRootViewController:youJi];
                
                nav1.title=@"首页";
                nav2.title=@"工作台";
                nav3.title=@"我的";
                
                [nav1.tabBarItem setSelectedImage:[UIImage imageNamed:@"首页1"]];
                [nav1.tabBarItem setImage:[UIImage imageNamed:@"首页"]];
                
                [nav2.tabBarItem setSelectedImage:[UIImage imageNamed:@"工作台1"]];
                [nav2.tabBarItem setImage:[UIImage imageNamed:@"工作台"]];
                
                [nav3.tabBarItem setSelectedImage:[UIImage imageNamed:@"我的1"]];
                [nav3.tabBarItem setImage:[UIImage imageNamed:@"我的"]];
                
                [[UITabBar appearance] setTintColor:_backgroundColor];
                
                UITabBarController *bar=[[UITabBarController alloc] init];
                bar.viewControllers=@[nav1,nav2,nav3];
                
                self.window.rootViewController=bar;
                [self.window makeKeyAndVisible];
              
            } anderrorBlock:nil];
        }
        else{
            NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"user_role",token,@"token",waiter_id,@"waiter_id", nil];
            [ZQTools AFNPostDataUrl:@"waiter/logreg/transform" Dict:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
                [NSKeyedArchiver archiveRootObject:responseObject toFile:_userModelFile];
                homeViewController *shouye=[[homeViewController alloc] init];
                
                workbenchViewController *huDong=[[workbenchViewController alloc] init];
                
                myViewController *youJi=[[myViewController alloc] init];

                UINavigationController *nav1= [[UINavigationController alloc] initWithRootViewController:shouye];
                UINavigationController *nav2= [[UINavigationController alloc] initWithRootViewController:huDong];
                UINavigationController *nav3= [[UINavigationController alloc] initWithRootViewController:youJi];
                
                nav1.title=@"首页";
                nav2.title=@"工作台";
                nav3.title=@"我的";
                
                [nav1.tabBarItem setSelectedImage:[UIImage imageNamed:@"首页1"]];
                [nav1.tabBarItem setImage:[UIImage imageNamed:@"首页"]];
                
                [nav2.tabBarItem setSelectedImage:[UIImage imageNamed:@"工作台1"]];
                [nav2.tabBarItem setImage:[UIImage imageNamed:@"工作台"]];
                
                [nav3.tabBarItem setSelectedImage:[UIImage imageNamed:@"我的1"]];
                [nav3.tabBarItem setImage:[UIImage imageNamed:@"我的"]];
                
                [[UITabBar appearance] setTintColor:_backgroundColor];
                
                UITabBarController *bar=[[UITabBarController alloc] init];
                bar.viewControllers=@[nav1,nav2,nav3];
                
                self.window.rootViewController=bar;
                [self.window makeKeyAndVisible];
            } anderrorBlock:nil];
        }
    }


}

- (void)clearFile
{
    NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];
    NSArray * files = [[NSFileManager defaultManager ] subpathsAtPath :cachePath];
    //NSLog ( @"cachpath = %@" , cachePath);
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        //获取文件全路径
        NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent :p];
        if ([[NSFileManager defaultManager ] fileExistsAtPath :fileAbsolutePath]) {
            [[NSFileManager defaultManager ] removeItemAtPath :fileAbsolutePath error :&error];
          
        }
    }
    [ZQTools svpInfo:@"清除缓存成功"];

    //读取缓存大小
//        float cacheSize = [self readCacheSize] *1024; 
    //    self.cacheSize.text = [NSString stringWithFormat:@"%.2fKB",cacheSize];
    
}

-(void)creatAltel{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"是否要清除缓存" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction: [UIAlertAction actionWithTitle: @"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        [self clearFile];
    }]];
    [self.viewController presentViewController:alertController animated:YES completion:nil];
}
-(IBAction)pushToLogin:(UIButton *)tap{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"是否要退出登陆" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction: [UIAlertAction actionWithTitle: @"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        
        
      
        NSFileManager *defaultManager = [NSFileManager defaultManager];
        if ([defaultManager removeItemAtPath:_userModelFile error:nil]) {
            [self.viewController.navigationController popViewControllerAnimated:YES];
            //退出的时候删除别名
            [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                if (iResCode == 0) {
                    [self.viewController.navigationController popViewControllerAnimated:YES];
                    [ZQTools svpInfo:@"你已经退出登录状态"];
                   
                }
            } seq:1];
            
           
        }
        
    }]];
    [self.viewController presentViewController:alertController animated:YES completion:nil];
    
    
}


@end
